<?php

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

class HT_Model_administrator_models_seoer extends Zend_Db_Table {//ten class fai viet hoa

	protected $_db;

	public function __construct() {
		$this->_name = "seoer";
		$this->_db = Zend_Registry::get('dbMain');
		parent::init();
	}

	public function addData($data){
		$this->insert($data);
		return $this->getMaxId();
	}

	public function getMaxId(){
		$sql = "SELECT MAX(seoer_id) FROM seoer";
		return  (int)$this->_db->fetchOne($sql);
	}
	public function getSeoer($typeId,$filter = array()) {
		$sql = "SELECT seoer.*,us.*
				FROM seoer
				INNER JOIN user us ON us.wishid = seoer.wishid
				WHERE  seoer_id= ".(int)$typeId;
		return $this->_db->fetchRow($sql);
	}
	public function getListSeoer_nb($filter = array()) {
		$sqlPlus = $this->getListSeoer_sqlPlus($filter);
		$sql = "SELECT COUNT(seoer_id)
		FROM seoer
		INNER JOIN user us ON us.wishid = seoer.wishid
		WHERE 1=1 $sqlPlus";
		return $this->_db->fetchOne($sql);
	}
	public function getListSeoer($start=0,$size = 10,$filter = array()) {
		$sqlPlus = $this->getListSeoer_sqlPlus($filter);
		$sql = "SELECT seoer.*,us.user_name,us.email,CONCAT(us.firstname,' ',us.lastname) fullname,us.avatar,us.birthday
		FROM seoer
		INNER JOIN user us ON us.wishid = seoer.wishid
		WHERE 1=1 $sqlPlus ORDER BY us.email LIMIT $start,$size";
		return $this->_db->fetchAll($sql);
	}

	private function getListSeoer_sqlPlus($filter){
		$sqlPlus = null;
		foreach((array)$filter as $key => $val){
			$key = trim($key);
			$val = trim(addslashes($val));
			switch($key){
				case 'keyword':
					$sqlPlus .= " AND (us.user_name LIKE '%$val%' OR us.email LIKE '%$val%' OR us.firstname LIKE '%$val%' OR us.lastname LIKE '%$val%' OR seoer.description LIKE '%$val%') ";
					break;
				case 'seoer_status':
					$sqlPlus .= " AND seoer_status = ".(int) $val;
					break;
			}
		}
		return $sqlPlus;
	}

}

?>
